﻿// -----------------------------------------------------------------------
// <copyright file="RegexParser.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace FxCopContrib.Experimental.RegexCop
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Reflection;
    using System.Collections;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RegexParser
    {
        private static readonly Assembly RegexAssembly = typeof(Regex).Assembly;
        private const string RegexParserTypeFullName = "System.Text.RegularExpressions.RegexParser";
        private static readonly Type RegexParserType = RegexAssembly.GetType(RegexParserTypeFullName, true);
        private static readonly MethodInfo RegexParserParseMethod = RegexParserType.GetMethod("Parse", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(string), typeof(RegexOptions) }, null);

        public RegexTree Parse(string re, RegexOptions op)
        {
            return ParseInternal(re, op);
        }
        
        //internal static RegexReplacement ParseReplacement(string rep, RegexTree regex);
    
        public RegexTree ParseInternal(string expression, RegexOptions options)
        {
            try
            {
                return new RegexTree(RegexParserParseMethod.Invoke(null, new object[] { expression, options }));
            }
            catch (TargetInvocationException tie)
            {
                throw tie.InnerException;
            }
        }
    }
}
